package com.bonana.core.services.impl;

import com.bonana.core.dao.utils.PlatformMapper;
import com.bonana.core.entity.mybatis.Platform;
import com.bonana.core.entity.mybatis.PlatformExample;
import com.bonana.core.form.PlatformForm;
import com.bonana.core.services.UtilsService;
import com.bonana.core.utils.FileInfo;
import com.bonana.core.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/7/12.
 */
@Service
public class UtilsServiceImpl implements UtilsService {
    @Autowired
    PlatformMapper platformMapper;

    @Override
    public int SavePlatForm(PlatformForm backendFrom) {
        Platform saveForm = new Platform();
        if(backendFrom.getIconFile()!=null) {
            FileInfo fileInfo = OssUtils.putFile(backendFrom.getIconFile());
            saveForm.setPlatformIcon(fileInfo.getUrl());
        }
        saveForm.setPlatformId(backendFrom.getPlatformId());
        saveForm.setPlatformName(backendFrom.getPlatformName());
        saveForm.setPlatformUrl(backendFrom.getPlatformUrl());
        if(saveForm.getPlatformId() == 0) {
            return AddPlatForm(saveForm);
        } else {
            return platformMapper.updateByPrimaryKeySelective(saveForm);
        }
    }

    @Override
    public int AddPlatForm(Platform newForm) {
        return platformMapper.insert(newForm);
    }

    @Override
    public int RemovePlatform(int formId) {
        return platformMapper.deleteByPrimaryKey(formId);
    }

    @Override
    public Platform getPlatFormById(int formId) {
        return platformMapper.selectByPrimaryKey(formId);
    }

    @Override
    public List<Platform> getPlatform() {
        return platformMapper.selectByExample(new PlatformExample());
    }
}
